﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using TeacherLeftHand.Model;
using TeacherLeftHand.Util;

namespace CustomControl
{
    public class BlankRichTextEditor : RichTextEditor
    {
        public BlankRichTextEditor()
            : base()
        {
            this.IsDocumentEnabled = true;
            this.AddHandler(RichTextBox.DropEvent, new DragEventHandler(Rtb_Drop), true);
            this.AddHandler(RichTextBox.DragEnterEvent, new DragEventHandler(Rtb_DragEnter), true);
            this.AddHandler(RichTextBox.DragOverEvent, new DragEventHandler(Rtb_DragOver), true);
            //this.AddHandler(RichTextBox.MouseMoveEvent, new DragEventHandler(Rtb_MouseMove));
        }

        //private void Rtb_MouseMove(object sender, DragEventArgs e)
        //{
        //    Point docPoint = XtraRichEdit.Utils.Units.PixelsToDocuments(e.Location, richEditControl1.DpiX, richEditControl1.DpiY);
        //    DocumentPosition pos = richEditControl1.GetPositionFromPoint(docPoint);
        //    if (pos != null)
        //    {
        //        richEditControl1.Document.CaretPosition = pos;
        //        // ...
        //    }
        //}
        /// <remarks>
        /// GetCharIndexFromPosition is missing one caret position, as there is one extra caret
        /// position than there are characters (an extra one at the end).
        /// </remarks>
        //private int GetCaretIndexFromPoint(RichTextEditor box, int x, int y)
        //{
        //    //Point realPoint = box.GetPositionFromPoint(, true);
          
        //    //if (index == box.Text.Length - 1)
        //    //{
        //    //    Point caretPoint = box.GetPositionFromCharIndex(index);
        //    //    if (realPoint.X > caretPoint.X)
        //    //    {
        //    //        index += 1;
        //    //    }
        //    //}
        //    //return index;
        //}
        private void Rtb_DragOver(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(KnowledgeItem)))
            {
                e.Effects = DragDropEffects.Copy;
            }
            else if (e.Data.GetDataPresent(typeof(RichTextEditor)))
            {
                e.Effects = DragDropEffects.Copy;
            }
        }

        private void Rtb_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(KnowledgeItem)))
            {
                e.Effects = DragDropEffects.Copy;
            }
            else if (e.Data.GetDataPresent(typeof(RichTextEditor)))
            {
                e.Effects = DragDropEffects.Copy;
            }
        }

        private void Rtb_Drop(object sender, DragEventArgs e)
        {
            var tempRtb = new RichTextEditor();
            if (e.Data.GetDataPresent(typeof (KnowledgeItem)))
            {
                var droppedValue = (KnowledgeItem) e.Data.GetData(typeof (KnowledgeItem));
                Utility.LoadRTF(droppedValue.Content, tempRtb);
                ((RichTextEditor) sender).CaretPosition.InsertTextInRun(tempRtb.Text);
                foreach (var variable in tempRtb.Document.Blocks.ToList())
                {
                    ((RichTextEditor) sender).Document.Blocks.Add(variable);
                }

            }
            if (e.Data.GetDataPresent(typeof (RichTextEditor)))
            {
                var droppedValue = (RichTextEditor) Utility.CloneElement((RichTextEditor) e.Data.GetData(typeof (RichTextEditor)));

                var rtbSender = (RichTextEditor) sender;
                foreach (Block b in droppedValue.Document.Blocks)
                {
                    if (b is Paragraph)
                    {
                        foreach (var line in ((Paragraph)b).Inlines)
                        {
                            if(line is Run)
                            {
                                rtbSender.AppendText((((Run)line).Text));
                                rtbSender.CaretPosition = rtbSender.CaretPosition.DocumentEnd;
                            }
                            //else if(line is InlineUIContainer)
                            //{
                            //    if(((InlineUIContainer)line).Child is InlineRichTextBox)
                            //    {
                            //        new InlineUIContainer(new InlineRichTextBox(), rtbSender.CaretPosition);
                            //        rtbSender.CaretPosition = rtbSender.CaretPosition.DocumentEnd;
                            //    }
                            //}
                        }
                    }
                }
            }
        }
  

    }
}
